package com.stylefeng.guns.modular.pay.service.impl;

import static com.baidu.unbiz.fluentvalidator.ResultCollectors.toSimple;

import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baidu.unbiz.fluentvalidator.FluentValidator;
import com.baidu.unbiz.fluentvalidator.Result;
import com.baidu.unbiz.fluentvalidator.jsr303.HibernateSupportedValidator;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.base.Strings;
import com.stylefeng.guns.modular.pay.dao.BankLimitMapper;
import com.stylefeng.guns.modular.pay.dao.CardMapper;
import com.stylefeng.guns.modular.pay.dao.MemberMapper;
import com.stylefeng.guns.modular.pay.dto.ApiCardParams;
import com.stylefeng.guns.modular.pay.exception.ApiException;
import com.stylefeng.guns.modular.pay.model.BankLimit;
import com.stylefeng.guns.modular.pay.model.Card;
import com.stylefeng.guns.modular.pay.model.MchReg;
import com.stylefeng.guns.modular.pay.model.Member;
import com.stylefeng.guns.modular.pay.service.ICardService;
import com.stylefeng.guns.modular.pay.service.IMchRegService;

/**
 * <p>
 * 银行卡 服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2018-09-26
 */
@Service
public class CardServiceImpl extends ServiceImpl<CardMapper, Card> implements ICardService {
	private static Validator validator;
	@Autowired
	private CardMapper cardMapper;
	@Autowired
	private MemberMapper memberMapper;
	@Autowired
	private BankLimitMapper bankLimitMapper;
	@Autowired
	private IMchRegService mchRegService;
	
	static {
		Locale.setDefault(Locale.ENGLISH); // speicify language
		ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
		validator = factory.getValidator();
	}
	

	@Override
	public void bind(Integer memberId, String accountName, Integer bankId, String cardNo, String phone) {
		if(memberId==null){
			throw new ApiException("empty.memberId","会员id不能为空");
		}
		if(bankId==null){
			throw new ApiException("empty.bankId","开户行不能为空");
		}
		if(Strings.isNullOrEmpty(accountName)){
			throw new ApiException("empty.accountName","账户名不能为空");
		}
		if(Strings.isNullOrEmpty(cardNo)){
			throw new ApiException("empty.cardNo","卡号不能为空");
		}
		if(Strings.isNullOrEmpty(phone)){
			throw new ApiException("empty.phone","手机号不能为空");
		}
//		Member member = memberMapper.selectById(memberId);
		BankLimit bank = bankLimitMapper.selectById(bankId);
		if(bank==null){
			throw new ApiException("empty.bank","开户行不存在");
		}
		Card card = new Card();
		card.setAccountName(accountName);
		card.setBankName(bank.getBankName());
		card.setCardNo(cardNo);
		card.setBankId(bank.getId());
		card.setCreateTime(new Date());
		card.setMemberId(memberId);
		card.setPhone(phone);
		cardMapper.insert(card);
	}


	@Override
	public void bind(ApiCardParams params) {
		//业务校验
		Result ret = FluentValidator.checkAll()
				.on(params, new HibernateSupportedValidator<ApiCardParams>().setHiberanteValidator(validator))
				.doValidate().result(toSimple());
		if (!ret.isSuccess()) {
			String msg = ret.getErrors().get(0);
			throw new ApiException("validate_error",msg);
		}
		Member member = memberMapper.selectById(Integer.parseInt(params.getMemberId()));
		if(member==null){
			throw new ApiException("member_error","会员不存在");
		}
		//验证商户是否认证
		if(member.getAuthStatus()==null || member.getAuthStatus()!=2){
			throw new ApiException("auth_error","商户尚未认证，请先认证");
		}
		MchReg mchReg = mchRegService.findByMchNo(member.getMemberNo());
		if(!params.getAccountName().equals(mchReg.getRealName())){
			throw new ApiException("auth_error","绑定卡账户名与认证姓名不一致");
		}
		//验证卡号是否已经存在防止重复帮卡
		List<Card> temcards = cardMapper.selectList(new EntityWrapper<Card>().eq("card_no", params.getCardNo()));
		if(temcards!=null &&temcards.size()>0){
			throw new ApiException("auth_error","该卡已绑定，重复绑定");
		}
		BankLimit bank = bankLimitMapper.selectById(params.getBankId());
		//绑定卡姓名和认证名称一致
		Card card = new Card();
		card.setAccountName(params.getAccountName());
		card.setBankName(bank.getBankName());
		card.setCardNo(params.getCardNo());
		card.setBankId(bank.getId());
		card.setCreateTime(new Date());
		card.setMemberId(Integer.parseInt(params.getMemberId()));
		card.setPhone(params.getPhone());
		cardMapper.insert(card);		
	}

}
